const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 金额换算
 * @param money 金额数字
 * @param direction 换算方向：-1 元至分；1 分至元
 * @returns 换算后的金额
 */
const moneyConvert = (money, direction = 1) => {
  if (typeof money !== 'number') return 0;
  let result = 0;
  if (direction < 0) {
    result = parseFloat((money * 100).toFixed(2));
  } else if (direction > 0) {
    result = parseFloat((money / 100).toFixed(2));
  }
  console.log(money, result);
  return result;
}

const getCurrentPageUrl = () => {
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  const url = `/${currentPage.route}`
  return url
}

/**
 * 将字符串里的非数字字符全部替换为''
 */
const validateNumber = (val) => {
  return val.replace(/\D/g, '')
}

/**
 * 将字符串里的空格全部替换为''
 */
const validateNull = (val) => {
  var str = val.replace(/[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig, "");
  return str.replace(/\s+/g, "");
}

module.exports = {
  formatTime,
  getCurrentPageUrl,
  validateNumber,
  moneyConvert,
  validateNull
}